package com.museum.data;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.museum.model.Location;
import com.museum.model.Mp_User_Visit;
import com.museum.model.Open_Date;
import com.museum.model.Open_Date_Item;
import com.museum.model.Place;
import com.museum.model.Visited;
import com.museum.model.Watched;

public class Mp_User_Visit_Handler {
	private Mp_User_Visit _mp_user_visit;
	private Visited visited;
	private Watched watched;
	private Place place_visited;
	private Place place_watched;
	
	private String _username;
	private String _password;
	
	XmlRpc xmlrpc = new XmlRpc();
	public Mp_User_Visit_Handler(String username, String password){
		_username = username;
		_password = password;
		
		visited = new Visited();
		watched = new Watched();
		_mp_user_visit = new Mp_User_Visit();
		this.Parse();
		_mp_user_visit.setVisited(visited);
		_mp_user_visit.setWatched(watched);
		
	}
	public Mp_User_Visit get_Mp_User_Visit(){		
		return _mp_user_visit;
	}
	private void Parse(){		
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("username", _username);
      	params.put("password", _password); 
      	
      	Map<String, Object> response = xmlrpc.call("mp.user.visit", params);
		
		for (Map.Entry<String, Object> entry : response.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();	            
            
            Map<String, Object> response2 = (Map<String, Object>)value;
            //Loop2
            for (Map.Entry<String, Object> entry2 : response2.entrySet()) {
	            String key2 = entry2.getKey();
	            Object value2 = entry2.getValue();
	            	
	            if(key.equals("visited")){		            	
	            	place_visited = new Place();
	            	place_visited.setPlaceId(key2);
	            	//Log.d("visited", value2.toString());
	            	ParseVisited(value2);
	            }//End if(key.equals("visited"))
	            else if(key.equals("watched")){
	            	place_watched = new Place();
	            	place_watched.setPlaceId(key2);
	            	//Log.d("watched", value2.toString());
	            	ParseWatched(value2);
	            }
            }//End Loop2
        }//End Loop1
	}
	private void ParseVisited(Object obj){
		//Loop3
        Map<String, Object> response3 = (Map<String, Object>)obj;
        for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            if(key3.equals("email")){
            	place_visited.setEmail(value3.toString());
            }else if(key3.equals("website")){
            	place_visited.setWebsite(value3.toString());
            }else if(key3.equals("added_timestamp")){
            	place_visited.setAdded_timestamp(value3.toString());
            }else if(key3.equals("description")){
            	place_visited.setDescription(value3.toString());
            }else if(key3.equals("name")){
            	place_visited.setName(value3.toString());
	      	}else if(key3.equals("create_timestamp")){
	      		place_visited.setCreate_timestamp(value3.toString());
	        }else if(key3.equals("status")){
	        	place_visited.setStatus(value3.toString());
	        }else if(key3.equals("exhibition_status")){
	        	place_visited.setExhibition_status(value3.toString());
	        }
	        else if(key3.equals("location")){
	        	Location location = new Location();
	            Map<String, Object> response4 = (Map<String, Object>)value3;
			    for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			         String key4 = entry4.getKey();
				     Object value4 = entry4.getValue();								         
				        
				     if(key4.equals("longitude")){
				    	 location.setLongitude(value4.toString());
				     }else if(key4.equals("description")){
				        location.setDescription(value4.toString());
				     }else if(key4.equals("latitude")){
				        location.setLatitude(value4.toString());
				     }								        
			    }//End Loop4
			    place_visited.setLocation(location);
	        }else if(key3.equals("open_date")){
	        	Open_Date opendate = new Open_Date();
	        	Map<String, Object> response4 = (Map<String, Object>)value3;
			        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			           	String key4 = entry4.getKey();
				        Object value4 = entry4.getValue();
				        
				        Open_Date_Item opendateitem = new Open_Date_Item();
				        opendateitem.setDate(key4);
					    Map<String, Object> response5 = (Map<String, Object>)value4;
						for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
							String key5 = entry5.getKey();
							Object value5 = entry5.getValue();									        
								        
							if(key5.equals("close_time")){
								opendateitem.setClose_time(value5.toString());
							}else if(key5.equals("open_time")){
								opendateitem.setOpen_time(value5.toString());
							}							
						}//End Loop5
						opendate.addItem(opendateitem);
						place_visited.setOpen_date(opendate);
			        }//End Loop4
	          }//End if
        }//End Loop3
        visited.addItem(place_visited);
	}
	private void ParseWatched(Object obj){
		//Loop3
        Map<String, Object> response3 = (Map<String, Object>)obj;
        for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            if(key3.equals("email")){
            	place_watched.setEmail(value3.toString());
            }else if(key3.equals("website")){
            	place_watched.setWebsite(value3.toString());
            }else if(key3.equals("added_timestamp")){
            	place_watched.setAdded_timestamp(value3.toString());
            }else if(key3.equals("description")){
            	place_watched.setDescription(value3.toString());
            }else if(key3.equals("name")){
            	place_watched.setName(value3.toString());
	      	}else if(key3.equals("create_timestamp")){
	      		place_watched.setCreate_timestamp(value3.toString());
	        }else if(key3.equals("exhibition_status")){
	        	place_watched.setExhibition_status(value3.toString());
	        }
	        else if(key3.equals("location")){
	        	Location location = new Location();
	            Map<String, Object> response4 = (Map<String, Object>)value3;
			    for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			         String key4 = entry4.getKey();
				     Object value4 = entry4.getValue();								         
				        
				     if(key4.equals("longitude")){
				    	 location.setLongitude(value4.toString());
				     }else if(key4.equals("description")){
				        location.setDescription(value4.toString());
				     }else if(key4.equals("latitude")){
				        location.setLatitude(value4.toString());
				     }								        
			    }//End Loop4
			    place_watched.setLocation(location);
	        }else if(key3.equals("open_date")){
	        	Open_Date opendate = new Open_Date();
	        	Map<String, Object> response4 = (Map<String, Object>)value3;
			        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			           	String key4 = entry4.getKey();
				        Object value4 = entry4.getValue();
				        
				        Open_Date_Item opendateitem = new Open_Date_Item();
				        opendateitem.setDate(key4);
					    Map<String, Object> response5 = (Map<String, Object>)value4;
						for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
							String key5 = entry5.getKey();
							Object value5 = entry5.getValue();									        
								        
							if(key5.equals("close_time")){
								opendateitem.setClose_time(value5.toString());
							}else if(key5.equals("open_time")){
								opendateitem.setOpen_time(value5.toString());
							}							
						}//End Loop5
						opendate.addItem(opendateitem);
						place_watched.setOpen_date(opendate);
			        }//End Loop4
	          }//End if
        }//End Loop3
        watched.addItem(place_watched);
	}
}
